#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Date:2022/09/09 16:22:30
'''
import sys
sys.path.append('.')


from db_logic.db_base import Repository

from models.medicament_models import EntityMedicamentRelation

from Common.Utils import Utils


class BllMedicamentRelation(Repository):
    def __init__(self, entityType=EntityMedicamentRelation):
        return super().__init__(entityType)

    def get_seach_list(self, seach_word, page_param):
        filter_base = ""
        if seach_word:
            seach_word = f"%{seach_word}%"
            filter_base += f" (name1 like '{seach_word}' or name2 like '{seach_word}' or relation like '{seach_word}') "

        if filter_base:
            filter_base = f" where {filter_base}"
        sql_all = f"""
            select * from rms_medicament_relation {filter_base}
        """
        try:
            count_number = len(self.execute(sql_all).fetchall())

        except:
            count_number = 0
        page_param.totalRecords = count_number
        page_sql = Utils.sql_paging_assemble(sql_all, page_param)
        return self.execute(page_sql).fetchall()